﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;

namespace DotNet.Helper
{
    /// <summary>
    /// log4net日志帮助类
    /// </summary>
    public class Log4netHelper
    {
        private static Log4netHelper _instance = null;
        private static ILog _log;

        /// <summary>
        /// 日志属性
        /// </summary>
        public static Log4netHelper Log
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Log4netHelper();
                    string cfgPath = AppDomain.CurrentDomain.BaseDirectory + @"\log4net.config";
                    XmlConfigurator.Configure(new Uri(cfgPath));
                    _log = LogManager.GetLogger("Log");
                }
                return _instance;
            }
        }

        /// <summary>
        /// 写普通信息
        /// </summary>
        /// <param name="msg">消息</param>
        public void WriteInfo(string msg)
        {
            _log.Info(msg);
        }

        /// <summary>
        /// 写调试信息
        /// </summary>
        /// <param name="msg">消息</param>
        public void WriteDebug(string msg)
        {
            _log.Debug(msg);
        }

        /// <summary>
        /// 写错误信息
        /// </summary>
        /// <param name="msg">消息</param>
        public void WriteError(string msg)
        {
            _log.Error(msg);
        }

        /// <summary>
        /// 写错误信息
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="ex">错误信息</param>
        public void WriteError(string msg, Exception ex)
        {
            _log.Error(msg, ex);
        }
    }
}
